package pers.course.week7;

import java.util.Scanner;

class NP {
    private final String name;
    private int final_money_value = 0;
    private int init_money_value;
    private int rest_money_value;
    private String[] receiveNP;
    public NP(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setInit_money(int init_money) {
        this.init_money_value = init_money;
    }
    public void setReceiveNP(String[] receiveNP) {
        this.receiveNP = receiveNP;
    }
    public int getGift_value() {
        return init_money_value / receiveNP.length;
    }
    public int getInit_money_value() {
        return init_money_value;
    }
    public String[] getReceiveNP() {
        return receiveNP;
    }
    public void addMoney(int gift_value) {
        final_money_value += gift_value;
    }
    public int getFinal_money_value() {
        return final_money_value;
    }
    public void setRest_money_value(int rest_money_value) {
        this.rest_money_value = rest_money_value;
    }
    public int getRest_money_value() {
        return rest_money_value;
    }
}

public class GiftGivers{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int nums = in.nextInt();
        NP[] np = new NP[nums];
        for (int i = 0; i < nums; i++) {
            np[i] = new NP(in.next());
        }
        for (int i = 0; i < nums; i++) {
            String giver = in.next();
            for (NP np1 : np) {
                if (np1.getName().equals(giver)) {
                    np1.setInit_money(in.nextInt());
                    int receiverNum = in.nextInt();
                    String[] reciever = new String[receiverNum];
                    for (int j = 0; j < receiverNum; j++) {
                        reciever[j] = in.next();
                    }
                    np1.setReceiveNP(reciever);
                }
            }
        }
        giveGifts(np);
        for (NP np1 : np) {
            System.out.println(np1.getName() + " " + (np1.getRest_money_value() + np1.getFinal_money_value() - np1.getInit_money_value()));
        }
    }
    public static void giveGifts(NP[] np) {
        for (NP np1 : np) {
            if (np1.getReceiveNP().length != 0) {
                int gift_value = np1.getGift_value();
                String[] reciever = np1.getReceiveNP();
                for (String s : reciever) {
                    for (NP np2 : np) {
                        if (s.equals(np2.getName())) {
                            np2.addMoney(gift_value);
                        }
                    }
                }
                np1.setRest_money_value(np1.getInit_money_value() - (gift_value * reciever.length));
            }
        }
    }
}
